package com.tomorrowbbs.service.impl;

import java.util.Date;
import java.util.List;

import com.tomorrowbbs.domain.*;
import com.tomorrowbbs.domain.dto.SessionWebUserDto;
import com.tomorrowbbs.domain.enums.MessageStatusEnum;
import com.tomorrowbbs.domain.enums.MessageTypeEnum;
import com.tomorrowbbs.exception.BusinessException;
import com.tomorrowbbs.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tomorrowbbs.service.IForumArticleAttachmentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 文件信息Service业务层处理
 * 
 * @author zjz
 * @date 2024-11-09
 */
@Service
public class ForumArticleAttachmentServiceImpl implements IForumArticleAttachmentService 
{
    @Autowired
    private ForumArticleAttachmentMapper forumArticleAttachmentMapper;
    @Autowired
    private ForumArticleAttachmentDownloadMapper forumArticleAttachmentDownloadMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private ForumArticleMapper forumArticleMapper;
    @Autowired
    private UserMessageMapper userMessageMapper;
    /**
     * 查询文件信息
     * 
     * @param fileId 文件信息主键
     * @return 文件信息
     */
    @Override
    public ForumArticleAttachment selectForumArticleAttachmentByFileId(String fileId)
    {
        return forumArticleAttachmentMapper.selectForumArticleAttachmentByFileId(fileId);
    }

    /**
     * 查询文件信息列表
     * 
     * @param forumArticleAttachment 文件信息
     * @return 文件信息
     */
    @Override
    public List<ForumArticleAttachment> selectForumArticleAttachmentList(ForumArticleAttachment forumArticleAttachment)
    {
        return forumArticleAttachmentMapper.selectForumArticleAttachmentList(forumArticleAttachment);
    }

    /**
     * 新增文件信息
     * 
     * @param forumArticleAttachment 文件信息
     * @return 结果
     */
    @Override
    public int insertForumArticleAttachment(ForumArticleAttachment forumArticleAttachment)
    {
        return forumArticleAttachmentMapper.insertForumArticleAttachment(forumArticleAttachment);
    }

    /**
     * 修改文件信息
     * 
     * @param forumArticleAttachment 文件信息
     * @return 结果
     */
    @Override
    public int updateForumArticleAttachment(ForumArticleAttachment forumArticleAttachment)
    {
        return forumArticleAttachmentMapper.updateForumArticleAttachment(forumArticleAttachment);
    }

    /**
     * 批量删除文件信息
     * 
     * @param fileIds 需要删除的文件信息主键
     * @return 结果
     */
    @Override
    public int deleteForumArticleAttachmentByFileIds(String[] fileIds)
    {
        return forumArticleAttachmentMapper.deleteForumArticleAttachmentByFileIds(fileIds);
    }

    /**
     * 删除文件信息信息
     * 
     * @param fileId 文件信息主键
     * @return 结果
     */
    @Override
    public int deleteForumArticleAttachmentByFileId(String fileId)
    {
        return forumArticleAttachmentMapper.deleteForumArticleAttachmentByFileId(fileId);
    }

    /*
    * 文件下载*/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ForumArticleAttachment downloadAttachment(String fileId, SessionWebUserDto sessionWebUserDto) {
        ForumArticleAttachment forumArticleAttachment = forumArticleAttachmentMapper.selectForumArticleAttachmentByFileId(fileId);
        if(forumArticleAttachment == null){
            throw new BusinessException("文件不存在");
        }

        ForumArticleAttachmentDownload download = null;
        if(forumArticleAttachment.getIntegral()>0 && !sessionWebUserDto.getUserId().equals(forumArticleAttachment.getUserId())){
            download = forumArticleAttachmentDownloadMapper.selectForumArticleAttachmentDownloadByInfo(fileId,sessionWebUserDto.getUserId());
            if(download == null){
                UserInfo userInfo = userInfoMapper.selectUserInfoByUserId(sessionWebUserDto.getUserId());
                if (userInfo.getCurrentIntegral() - forumArticleAttachment.getIntegral() < 0){
                    throw new BusinessException("积分不足");
                }
            }
        }

        ForumArticleAttachmentDownload updateDownload = new ForumArticleAttachmentDownload();
        updateDownload.setFileId(fileId);
        updateDownload.setUserId(sessionWebUserDto.getUserId());
//        updateDownload.setDownloadCount(1l);

        ForumArticleAttachmentDownload download1 = forumArticleAttachmentDownloadMapper.selectForumArticleAttachmentDownloadByInfo(updateDownload.getFileId(),updateDownload.getUserId());

        if(download1 == null){
            updateDownload.setArticleId(forumArticleAttachment.getArticleId());
            updateDownload.setDownloadCount(1l);
            forumArticleAttachmentDownloadMapper.insertForumArticleAttachmentDownload(updateDownload);
        }else {
            updateDownload.setDownloadCount(download1.getDownloadCount()+1);
            updateDownload.setArticleId(download1.getArticleId());
            forumArticleAttachmentDownloadMapper.updateForumArticleAttachmentDownload(updateDownload);
        }


        // 更新下载次数
        forumArticleAttachmentMapper.updateDownloadCount(fileId);
        //是上传人下载
        if(sessionWebUserDto.getUserId().equals(forumArticleAttachment.getUserId()) || download != null){
            return forumArticleAttachment;
        }
        //扣除积分
        userInfoMapper.updateIntegral(sessionWebUserDto.getUserId(),-forumArticleAttachment.getIntegral());
        //为发布者增加积分
        userInfoMapper.updateIntegral(forumArticleAttachment.getUserId(),forumArticleAttachment.getIntegral());

        //记录消息
        ForumArticle forumArticle = forumArticleMapper.selectForumArticleByArticleId(forumArticleAttachment.getArticleId());

        UserMessage userMessage = new UserMessage();

        //通过发送者、接受者、当前文章、发送消息类型查询是否已经发送过了该消息

        userMessage.setMessageType(MessageTypeEnum.DOWNLOAD_ATTACHMENT.getType());
        userMessage.setReceivedUserId(forumArticle.getUserId());
        userMessage.setArticleId(forumArticle.getArticleId());
        userMessage.setSendUserId(sessionWebUserDto.getUserId());

        UserMessage userMessage1 = userMessageMapper.selectUserMessageByInfo(userMessage);
        System.out.println("aaaaa"+userMessage1);

        if(userMessage1 == null){
            userMessage.setCreateTime(new Date());
            userMessage.setArticleTitle(forumArticle.getTitle());
            userMessage.setCommentId(0l);
            userMessage.setSendNickName(sessionWebUserDto.getNickName());
            userMessage.setStatus(MessageStatusEnum.NO_READ.getStatus());
            userMessage.setMessageContent("用户"+sessionWebUserDto.getNickName()+"下载了你上传的附件："+forumArticleAttachment.getFileName());
            userMessageMapper.insertUserMessage(userMessage);
        }

        return forumArticleAttachment;
    }
}





























